using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using System.Security;
using System.Security.Principal;
using System.Web.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Administration
{
    public class CreateFAAdministratorPage : BaseMembershipPage
    {
        protected WebApplicationSelector Selector;
        protected InputFormTextBox TxtFirstName;
        protected InputFormTextBox TxtSurname;
        protected InputFormTextBox TxtUsername;
        protected InputFormTextBox TxtPassword;
        protected InputFormTextBox TxtConfirmPassword;
        protected InputFormTextBox TxtQuestion;
        protected InputFormTextBox TxtAnswer;
        protected InputFormTextBox TxtEmail;
        protected Label LblWebApp;
        protected Label AddUserError;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.LblWebApp.Text = WebAppUrl + " (Parent Web Application : " + WebApp.Name + ")";
        }

        protected void ValidatePassword(object sender, ServerValidateEventArgs args)
        {
            if (TxtPassword.Text != TxtConfirmPassword.Text)
                args.IsValid = false;
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;

            Page.Trace.Write("Starznet.CreateFAAdministratorPage",
                            "Using membership: " + Membership.Provider.Name + ", " +
                            Membership.Provider.ApplicationName);

            Page.Trace.Write("Starznet.CreateFAAdministratorPage",
                            "Creating user: " + this.TxtUsername.Text + ", " +
                            this.TxtPassword.Text + ", " +
                            this.TxtEmail.Text + ", " +
                            this.TxtQuestion.Text + ", " +
                            this.TxtAnswer.Text);

            MembershipCreateStatus oStatus;
            MembershipUser oUser = Membership.CreateUser(this.TxtUsername.Text,
                                  this.TxtPassword.Text,
                                  this.TxtEmail.Text,
                                  this.TxtQuestion.Text,
                                  this.TxtAnswer.Text,
                                  true,
                                  out oStatus);

            Page.Trace.Write("Starznet.CreateFAAdministratorPage", "Membership user create status: " + oStatus.ToString());

            if (oStatus == MembershipCreateStatus.Success)
            {
                //Find all forms authenticated web application
                foreach (SPAlternateUrl oURL in WebApp.AlternateUrls)
                {
                    if (WebApp.IisSettings[oURL.UrlZone].AuthenticationMode == AuthenticationMode.Forms)
                    {
                        SPPolicy oPolicy = WebApp.ZonePolicies(oURL.UrlZone).Add(oUser.ProviderName + ":" + oUser.UserName, oUser.UserName);
                        //oPolicy.IsSystemUser = true;
                        SPPolicyRole oRole = WebApp.PolicyRoles["Full Control"];
                        oPolicy.PolicyRoleBindings.Add(oRole);

                        Page.Trace.Write("Starznet.CreateFAAdministrator",
                                        "Add user to web users. UserName=" + oUser.UserName +
                                        ",Provider=" + oUser.ProviderName +
                                        ",Web=" + WebApp.Sites[0].RootWeb.Url);

                        WebApp.Sites[0].RootWeb.SiteUsers.Add(oUser.ProviderName + ":" + oUser.UserName,
                                                                oUser.Email,
                                                                oUser.UserName,
                                                                "");

                        SPUser oSPUser = WebApp.Sites[0].RootWeb.SiteUsers[oUser.ProviderName + ":" + oUser.UserName];
                        oSPUser.IsSiteAdmin = true;
                        oSPUser.Update();
                    }
                }

                Response.Redirect(PageToRedirectOnCancel);
            }
            else
            {
                AddUserError.Text = "Error creating user: " + oStatus.ToString();
            }
        }
    }
}